package com.sina.microblog.ut.cases
{
	import com.sina.microblog.MicroBlog;
	import com.sina.microblog.events.MicroBlogErrorEvent;
	import com.sina.microblog.events.MicroBlogEvent;
	import com.sina.microblog.ut.utils.AssertResult;
	import com.sina.microblog.ut.utils.ResultDataUtils;
	import com.sina.microblog.ut.utils.URLRequestChecker;
	import com.sina.microblog.ut.utils.URLRequestParameters;
	import com.sina.microblog.utils.StringEncoders;
	
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	import org.flexunit.asserts.assertTrue;
	import org.flexunit.async.Async;
	
	[RunWith("org.flexunit.experimental.theories.Theories")]
	public class RelationshipTest
	{
		private static const FRIENDSHIPS_CREATE_URL_BASE:String = "http://api.t.sina.com.cn/friendships/create/";
		private static const FRIENDSHIPS_CREATE_URL_WITHOUT_USER:String = "http://api.t.sina.com.cn/friendships/create.xml";
		private static const FRIENDSHIPS_DESTROY_URL_BASE:String = "http://api.t.sina.com.cn/friendships/destroy/";
		private static const FRIENDSHIPS_DESTROY_URL_WITHOUT_USER:String = "http://api.t.sina.com.cn/friendships/destroy.xml";
		private static const FRIENDSHIPS_SHOW_URL_BASE:String = "http://api.t.sina.com.cn/friendships/show.xml";
		
		private static const TIME_OUT:uint = 500;	
		private static var mb:MicroBlog;

		private static var testerID:uint = 132323;
		
		[DataPoints]
		[ArrayElementType("uint")]
		public static var ids:Array = [ 0, 1, 201, 300];
		
		[DataPoints]
		[ArrayElementType("String")]
		public static var names:Array = [ null, "tester1", "测试", "测试abc1="];
		
		public function RelationshipTest()
		{
		}
		
		[BeforeClass]
		public static function setUp():void
		{
			if ( null == mb )
			{
				mb = new MicroBlog();
			}
		}
		
		//Test Friendship Create.
		[Test(async,timeout="20000")]
		public function testFollow():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("user_id", testerID);
			params.addParam("follow", false);
			
			Async.handleEvent(this, mb, MicroBlogEvent.FOLLOW_RESULT, AssertResult.assertUser, TIME_OUT);
			
			var req:URLRequest = new URLRequest(FRIENDSHIPS_CREATE_URL_BASE + testerID.toString()+".xml");
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			req.data.screen_name = StringEncoders.urlEncodeUtf8String(names[1]);
			mb.follow(testerID.toString(),testerID, names[1], false);
			
			URLRequestChecker.check(mb, req, ResultDataUtils.user);			
			
		}
		[Test(async,timeout="20000")]
		public function testFollowFail():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			Async.handleEvent(this, mb, MicroBlogErrorEvent.FOLLOW_ERROR, AssertResult.assertError, TIME_OUT);
			
			var req:URLRequest = new URLRequest(FRIENDSHIPS_CREATE_URL_BASE + testerID.toString()+".xml");
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			mb.follow(testerID.toString(),0, null, true);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		[Test(async,timeout="20000")]
		public function testFollowWithoutUser():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("user_id", testerID);
			params.addParam("follow", false);
			
			Async.handleEvent(this, mb, MicroBlogEvent.FOLLOW_RESULT, AssertResult.assertUser, TIME_OUT);
			
			var req:URLRequest = new URLRequest(FRIENDSHIPS_CREATE_URL_WITHOUT_USER);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			req.data.screen_name = StringEncoders.urlEncodeUtf8String(names[1]);
			mb.follow(null,testerID, names[1], false);
			
			URLRequestChecker.check(mb, req, ResultDataUtils.user);			
			
		}
		//Test Friendship Destroy.
		[Test(async,timeout="20000")]
		public function testCancelFollow():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("user_id", testerID);
			params.addParam("screen_name", names[1]);
			
			Async.handleEvent(this, mb, MicroBlogEvent.CANCEL_FOLLOWING_RESULT, AssertResult.assertUser, TIME_OUT);
			
			var req:URLRequest = new URLRequest(FRIENDSHIPS_DESTROY_URL_BASE + testerID.toString()+".xml");
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			mb.cancelFollowing(testerID.toString(),testerID, names[1]);
			
			URLRequestChecker.check(mb, req, ResultDataUtils.user);			
			
		}
		[Test(async,timeout="20000")]
		public function testCancelFollowFail():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			Async.handleEvent(this, mb, MicroBlogErrorEvent.CANCEL_FOLLOWING_ERROR, AssertResult.assertError, TIME_OUT);
			
			var req:URLRequest = new URLRequest(FRIENDSHIPS_DESTROY_URL_BASE + testerID.toString()+".xml");
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			mb.cancelFollowing(testerID.toString(),0, null);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		[Test(async,timeout="20000")]
		public function testCancelFollowWithoutUser():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("user_id", testerID);
			params.addParam("screen_name", names[1]);
			
			Async.handleEvent(this, mb, MicroBlogEvent.CANCEL_FOLLOWING_RESULT, AssertResult.assertUser, TIME_OUT);
			
			var req:URLRequest = new URLRequest(FRIENDSHIPS_DESTROY_URL_WITHOUT_USER);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			mb.cancelFollowing(null,testerID, names[1]);
			
			URLRequestChecker.check(mb, req, ResultDataUtils.user);			
			
		}
		//Test Friendship Show.
		[Test(async,timeout="20000")]
		public function testFriendshipShow():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("target_id", testerID);
			params.addParam("target_screen_name", names[1]);
			params.addParam("source_id", testerID);
			params.addParam("source_screen_name", names[1]);
			Async.handleEvent(this, mb, MicroBlogEvent.CHECK_IS_FOLLOWING_RESULT, AssertResult.assertRelationship, TIME_OUT);
			
			var req:URLRequest = new URLRequest(FRIENDSHIPS_SHOW_URL_BASE + params.toGetString());
			mb.checkIsFollowing(testerID, names[1], testerID, names[1]);
			
			URLRequestChecker.check(mb, req, ResultDataUtils.relationship);			
			
		}
		[Test(async,timeout="20000")]
		public function testFriendshipShowFail():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("target_id", testerID);
			params.addParam("target_screen_name", names[1]);
			params.addParam("source_id", testerID);
			params.addParam("source_screen_name", names[1]);
			Async.handleEvent(this, mb, MicroBlogErrorEvent.CHECK_IS_FOLLOWING_ERROR, AssertResult.assertError, TIME_OUT);
			
			var req:URLRequest = new URLRequest(FRIENDSHIPS_SHOW_URL_BASE + params.toGetString());
			mb.checkIsFollowing(testerID, names[1], testerID, names[1]);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		[Theory]
		public function testFriendshipTheory(targetName:String, targetID:uint, sourceName:String, sourceID:uint):void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			var needToCheck:Boolean = false;
			if ( targetID > 0 )
			{
				params.addParam("target_id", targetID);
				needToCheck = true;
			}
			
			if ( targetName != null )
			{
				params.addParam("target_screen_name", StringEncoders.urlEncodeUtf8String( targetName) );
				needToCheck = needToCheck && true;
			}
			
			if ( sourceID > 0 )
			{
				params.addParam("source_id", sourceID);
			}
			
			if ( sourceName != null )
			{
				params.addParam("source_screen_name", StringEncoders.urlEncodeUtf8String(sourceName));
			}
			
			var req:URLRequest = new URLRequest(FRIENDSHIPS_SHOW_URL_BASE + params.toGetString());
			mb.checkIsFollowing(targetID, targetName, sourceID, sourceName);
			if ( ! needToCheck )
			{
				assertTrue(true);
			} else
			{
				URLRequestChecker.check(mb, req, null);
			}
		}
	}
}